Recap - addressing modes

Program in Memory (RAM)

The following are synonyms:

Stack and Heap memory

The diagram above is the entire memory allocated to the process by the operating system. How the OS allocates this virtual memory is a topic outside the scope of this course, but basically we can think of the memory as starting at 0 and going up to some large number, and not worry about other processes' concurrent memory usage.

Parts of memory:

Notes:

Another picture

Examples

// global var, so goes in the data section
double glob = 3.14

int main() {
    // local var of function main, so goes in stack
    int num = 0;

    // this instr goes in text section, along with rest of program code
    cout << num;

    // dynamic memory allocation
    // BUT ptr is a var so ptr goes on the stack
    // while the char array goes on the heap
    char* ptr = new char[10];

    // nums is a ptr to object or something
    vector<int> nums{1, 2, 3, 4};
}

Note: immediate values hardcoded like num = 0 are stored in program code (text section)

Rule: "stack is fast, heap is slow":